package com.lanren.familybookapp.ui.home.model;

import com.lanren.common.app.ApiUrl;
import com.lanren.common.base.impl.BaseModel;
import com.lanren.common.http.HttpManager;
import com.lanren.common.utils.GsonUtils;
import com.lanren.easyhttp.callback.SimpleCallBack;
import com.lanren.easyhttp.exception.ApiException;
import com.lanren.familybookapp.app.AppUserHelper;
import com.lanren.familybookapp.ui.home.bean.BookClassifyBean;
import com.lanren.familybookapp.ui.home.bean.BookPayTypeBean;
import com.lanren.familybookapp.ui.home.interfaces.IBookkeepingInfoModel;
import com.lanren.familybookapp.ui.me.bean.BookInfoBean;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.disposables.Disposable;

/**
 * @ClassName BookkeepingInfoModel
 * @Description TODO
 * @Author chongheng.wang
 * @Date 2020/2/14 13:45
 * @Version 1.0
 */
public class BookkeepingInfoModel extends BaseModel {

    public BookkeepingInfoModel() {
        initDispoable();
    }

    //获取账本详细
    public void getBookInfo(int bookId, final IBookkeepingInfoModel.BookInfoModel bookInfoModel) {

        Map<String, String> map = new LinkedHashMap<>();
        map.put("bookId", bookId + "");
        Disposable disposable = HttpManager.post(ApiUrl.API.BOOK_MANAGER_LIST)
                .headers(AppUserHelper.getHttpHeaders())
                .params(map)
                .execute(new SimpleCallBack<List<BookInfoBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        bookInfoModel.onError(e.getCode(), e.getMessage());
                    }

                    @Override
                    public void onSuccess(List<BookInfoBean> bookInfoBeans) {
                        bookInfoModel.onSuccess(bookInfoBeans);

                    }
                });

        addDisposable(disposable);

    }

    //更新修改账单信息
    public void updateBookkeeping(int id, int bookId, String bookClassify, String bookMoney, String bookInOut, String bookGoods,
                                  String bookTime, String bookMember, String bookPaytype, String bookPs,
                                  final IBookkeepingInfoModel.BookkeepingInfoModel bookkeepingEditModel) {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("id", id + "");
        map.put("createBookId", bookId + "");
        map.put("bookClassify", bookClassify);
        map.put("money", bookMoney);
        map.put("inOut", bookInOut);
        map.put("goods", bookGoods);
        map.put("bookTime", bookTime);
        map.put("bookMember", bookMember);
        map.put("paymentType", bookPaytype);
        map.put("ps", bookPs);

        Disposable disposable = HttpManager.post(ApiUrl.API.BOOK_BOOKKEEPING_UPDATE)
                .headers(AppUserHelper.getHttpHeaders())
                .upJson(GsonUtils.toJson(map))
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 200) {
                            bookkeepingEditModel.onSuccess(e.getMessage());
                        } else {
                            bookkeepingEditModel.onError(e.getCode(), e.getMessage());
                        }
                    }

                    @Override
                    public void onSuccess(String s) {
                        bookkeepingEditModel.onSuccess(s);
                    }
                });

        addDisposable(disposable);
    }

    //获取账单分类
    public void getBookClassify(String bookCreateUser, final IBookkeepingInfoModel.BookClassifyModel bookClassifyModel) {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("bookCreateUser", bookCreateUser);
        Disposable disposable = HttpManager.post(ApiUrl.API.BOOK_CLASSIFY_LIST)
                .headers(AppUserHelper.getHttpHeaders())
                .params(map)
                .execute(new SimpleCallBack<List<BookClassifyBean>>() {
                    @Override
                    public void onError(ApiException e) {

                        bookClassifyModel.onError(e.getCode(), e.getMessage());

                    }

                    @Override
                    public void onSuccess(List<BookClassifyBean> bookClassifyBeans) {
                        bookClassifyModel.onSuccess(bookClassifyBeans);

                    }

                });

        addDisposable(disposable);
    }

    public void getBookPaytypeList(String bookCreateUser, final IBookkeepingInfoModel.BookPaytypeModel bookPaytypeModel) {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("bookCreateUser", bookCreateUser);
        Disposable disposable = HttpManager.post(ApiUrl.API.BOOK_PAYTYPE_GET)
                .headers(AppUserHelper.getHttpHeaders())
                .params(map)
                .execute(new SimpleCallBack<List<BookPayTypeBean>>() {
                    @Override
                    public void onError(ApiException e) {

                        bookPaytypeModel.onError(e.getCode(), e.getMessage());
                    }

                    @Override
                    public void onSuccess(List<BookPayTypeBean> bookPayTypeBeans) {
                        bookPaytypeModel.onSuccess(bookPayTypeBeans);
                    }
                });
        addDisposable(disposable);
    }

    public void addBookPaytype(String bookCreateUser, String payType, final IBookkeepingInfoModel.BookkeepingInfoModel
            bookkeepingEditModel) {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("payType", payType);
        map.put("bookCreateUser", bookCreateUser);
        Disposable disposable = HttpManager.post(ApiUrl.API.BOOK_PAYTYPE_ADD)
                .headers(AppUserHelper.getHttpHeaders())
                .params(map)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 200) {
                            bookkeepingEditModel.onSuccess(e.getMessage());

                        } else {
                            bookkeepingEditModel.onError(e.getCode(), e.getMessage());

                        }
                    }

                    @Override
                    public void onSuccess(String s) {
                        bookkeepingEditModel.onSuccess(s);
                    }
                });
        addDisposable(disposable);
    }

    public void delBookPaytype(int payTypeId, final IBookkeepingInfoModel.BookkeepingInfoModel bookkeepingEditModel) {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("payTypeId", payTypeId + "");
        Disposable disposable = HttpManager.post(ApiUrl.API.BOOK_PAYTYPE_DEL)
                .headers(AppUserHelper.getHttpHeaders())
                .params(map)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 200) {
                            bookkeepingEditModel.onSuccess(e.getMessage());

                        } else {
                            bookkeepingEditModel.onError(e.getCode(), e.getMessage());

                        }
                    }

                    @Override
                    public void onSuccess(String s) {
                        bookkeepingEditModel.onSuccess(s);
                    }
                });
        addDisposable(disposable);
    }

    public void delBookInfo(int bookkeepingId, final IBookkeepingInfoModel.BookkeepingInfoModel bookkeepingEditModel) {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("bookkeepingId", bookkeepingId + "");
        Disposable disposable = HttpManager.post(ApiUrl.API.BOOK_BOOKKEEPING_DEL)
                .headers(AppUserHelper.getHttpHeaders())
                .params(map)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 200) {
                            bookkeepingEditModel.onSuccess(e.getMessage());

                        } else {
                            bookkeepingEditModel.onError(e.getCode(), e.getMessage());

                        }
                    }

                    @Override
                    public void onSuccess(String s) {
                        bookkeepingEditModel.onSuccess(s);
                    }
                });
        addDisposable(disposable);
    }


}
